package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.GetEveryDate;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.xml.crypto.Data;

import com.whw.fy.service.ScheduleService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Resource
    private ScheduleMapper scheduleMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private ScheduleDataNoMapper scheduleDataNoMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private PurchaseCourseRecordsMapper purchaseCourseRecordsMapper;
    @Resource
    private LessonMapper lessonMapper;

    @Override
    public Message deleteByPrimaryKey(Integer scheduleId) {
        return scheduleMapper.deleteByPrimaryKey(scheduleId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Schedule record) {
        return scheduleMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Schedule record) {
        return scheduleMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer scheduleId) {
        return Message.success(null).add(scheduleMapper.selectByPrimaryKey(scheduleId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Schedule record) throws ParseException {
        return scheduleMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Schedule record) {
        return scheduleMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectALL() {
        return Message.success(null).add(scheduleMapper.selectALL());
    }

    @Override
    public Message selectTodayScheduleByTeacherId(int TeacherId) {
        List<Schedule> scheduleList = scheduleMapper.selectByTeacherIdAndTimeDay(TeacherId, new Date());
        List<Map> mapList = new LinkedList<>();
        for (Schedule schedule : scheduleList) {
            HashMap hashMap = new HashMap();
            hashMap.put("schedule", schedule);
            ScheduleDataNo scheduleDataNo = scheduleDataNoMapper.selectByPrimaryKey(schedule.getTimeSkcd());
            hashMap.put("timeStart", scheduleDataNo.getScheduleDataNoDateTextStart());
            int timeEnd = schedule.getTimeSkcd() + schedule.getTimeSkjc() - 1;
            hashMap.put("timeEnd", scheduleDataNoMapper.selectByPrimaryKey(timeEnd).getScheduleDataNoDateTextEnd());
            hashMap.put("timeRange", scheduleDataNo.getScheduleDataNoRange());
            mapList.add(hashMap);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectTodayScheduleByStudentId(int studentId) {
        List<Schedule> scheduleList = new LinkedList<>();
        List<Clazz> clazzList = new LinkedList<>();
        List<Clazz> clazzallList = clazzMapper.selectByClassStatus(0);
        for (Clazz clazz : clazzallList) {
            String[] studentIds = clazz.getStudentId().split(";");
            if (studentIds.length > 0) {
                for (String studentIdString : studentIds) {
                    if (studentId == Integer.parseInt(studentIdString)) {
                        clazzList.add(clazz);
                        break;
                    }
                }
            }
        }
        if (clazzList.size() > 0) {
            Date now = new Date();
            for (Clazz clazz : clazzList) {
                List<Schedule> schedules = scheduleMapper.selectByClassIdAndTimeDay(clazz.getClassId(), now);
                if (schedules.size() > 0) {
                    for (Schedule schedule : schedules) {
                        scheduleList.add(schedule);
                    }
                }
            }
            List<Map> mapList = new LinkedList<>();
            for (Schedule schedule : scheduleList) {
                HashMap hashMap = new HashMap();
                hashMap.put("schedule", schedule);
                ScheduleDataNo scheduleDataNo = scheduleDataNoMapper.selectByPrimaryKey(schedule.getTimeSkcd());
                hashMap.put("timeStart", scheduleDataNo.getScheduleDataNoDateTextStart());
                int timeEnd = schedule.getTimeSkcd() + schedule.getTimeSkjc() - 1;
                hashMap.put("timeEnd", scheduleDataNoMapper.selectByPrimaryKey(timeEnd).getScheduleDataNoDateTextEnd());
                hashMap.put("timeRange", scheduleDataNo.getScheduleDataNoRange());
                mapList.add(hashMap);
            }
            return Message.success(null).add(mapList);
        } else return Message.success("该学生没有上课班级！");


    }

    @Override
    public Message selectappletByTeacherId(int teacherId) {
        List<Schedule> scheduleList = scheduleMapper.selectByTeacherIdAndTimeDayNow(teacherId);
        HashMap hashMap = new HashMap();
        List<Map> weekList = weekListInit();
        hashMap.put("weekList", weekList);
        List<Map> morning = dataNoRangeListInit("morning");
        hashMap.put("morning", morning);
        List<Map> afternoon = dataNoRangeListInit("afternoon");
        hashMap.put("afternoon", afternoon);
        List<Map> night = dataNoRangeListInit("night");
        hashMap.put("night", night);
        List<Map> tagList = AlltagListInit(teacherId);
        hashMap.put("tagList", tagList);
        List<Map> wlist = wlistInit(scheduleList);
        hashMap.put("wlist", wlist);
        return Message.success(null).add(hashMap);

    }

    @Override
    public Message selectappletByStudentId(int studentId) {
        List<Schedule> scheduleList = new LinkedList<>();
        List<Clazz> clazzList = new LinkedList<>();
        List<Clazz> clazzallList = clazzMapper.selectByClassStatus(0);
        for (Clazz clazz : clazzallList) {
            String[] studentIds = clazz.getStudentId().split(";");
            if (studentIds.length > 0) {
                for (String studentIdString : studentIds) {
                    if (studentId == Integer.parseInt(studentIdString)) {
                        clazzList.add(clazz);
                        break;
                    }
                }
            }
        }
        if (clazzList.size() > 0) {
            for (Clazz clazz : clazzList) {
                List<Schedule> schedules = scheduleMapper.selectByClassIdAndTimeDayNowInWeek(clazz.getClassId());
                if (schedules.size() > 0) {
                    for (Schedule schedule : schedules) {
                        scheduleList.add(schedule);
                    }
                }
            }
            return Message.success(null).add(scheduleList);
        } else return Message.success("该学生没有上课班级！");
    }

    @Override
    public Message appletselectappletByStudentId(int studentId) {
        List<Schedule> scheduleList = new LinkedList<>();
        List<Clazz> clazzList = new LinkedList<>();
        List<Clazz> clazzallList = clazzMapper.selectByStatus01();
        for (Clazz clazz : clazzallList) {
            String[] studentIds = clazz.getStudentId().split(";");
            if (studentIds.length > 0) {
                for (String studentIdString : studentIds) {
                    if (studentId == Integer.parseInt(studentIdString)) {
                        clazzList.add(clazz);
                        break;
                    }
                }
            }
        }
        if (clazzList.size() > 0) {
            for (Clazz clazz : clazzList) {
                List<Schedule> schedules = scheduleMapper.selectByClassIdAndTimeDayNowInWeek(clazz.getClassId());
                if (schedules.size() > 0) {
                    for (Schedule schedule : schedules) {
                        scheduleList.add(schedule);
                    }
                }
            }
            HashMap hashMap = new HashMap();
            List<Map> weekList = weekListInit();
            hashMap.put("weekList", weekList);
            List<Map> morning = dataNoRangeListInit("morning");
            hashMap.put("morning", morning);
            List<Map> afternoon = dataNoRangeListInit("afternoon");
            hashMap.put("afternoon", afternoon);
            List<Map> night = dataNoRangeListInit("night");
            hashMap.put("night", night);
            List<Map> wlist = wlistInit(scheduleList);
            hashMap.put("wlist", wlist);
            return Message.success(null).add(hashMap);
        } else return Message.success("该学生没有上课班级！");
    }


    @Override
    public Message selectApplet() {
        HashMap hashMap = new HashMap();
        List<Map> weekList = weekListInit();
        hashMap.put("weekList", weekList);
        List<Map> morning = dataNoRangeListInit("morning");
        hashMap.put("morning", morning);
        List<Map> afternoon = dataNoRangeListInit("afternoon");
        hashMap.put("afternoon", afternoon);
        List<Map> night = dataNoRangeListInit("night");
        hashMap.put("night", night);
        return Message.success(null).add(hashMap);
    }

    @Override
    public Message selectByClazzIdAndTime(int clazzId, String Time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(Time);
        List<String> daylist = GetEveryDate.dateToWeekComplete(date);

        List<Map> timeDataList = new LinkedList<>();
        int i = 1;
        for (String day : daylist) {
            List<Schedule> scheduleList = scheduleMapper.selectByClassIdAndTimeDay(clazzId, simpleDateFormat.parse(day));
            HashMap map1 = ManagementCurriculumPackageTimeData(i, scheduleList);
            i++;
            timeDataList.add(map1);
        }
        HashMap hashMap = new HashMap();
        hashMap.put("timeData", timeDataList);
        hashMap.put("titleData", ManagementCurriculumPackageTitleData());
        return Message.success(null).add(hashMap);
    }

    @Override
    public Message selectByStudentId(int studentId) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        List<String> daylist = GetEveryDate.dateToWeekComplete(date);
        List<Map> timeDataList = new LinkedList<>();
        List<Clazz> clazzList = new LinkedList<>();
        List<Clazz> clazzallList = clazzMapper.selectByStatus01();
        for (Clazz clazz : clazzallList) {
            String[] studentIds = clazz.getStudentId().split(";");
            if (studentIds.length > 0) {
                for (String studentIdString : studentIds) {
                    if (studentId == Integer.parseInt(studentIdString)) {
                        clazzList.add(clazz);
                        break;
                    }
                }
            }
        }
        if (clazzList.size() > 0) {
            int i = 1;
            for (String day : daylist) {
                List<Schedule> allSchedule = new LinkedList<>();
                for (Clazz clazz : clazzList) {
                    List<Schedule> scheduleList = scheduleMapper.selectByClassIdAndTimeDay(clazz.getClassId(), simpleDateFormat.parse(day));
                    for (Schedule schedule : scheduleList) {
                        allSchedule.add(schedule);
                    }
                }
                HashMap map1 = ManagementCurriculumPackageTimeData(i, allSchedule);
                timeDataList.add(map1);
                i++;
            }
            HashMap hashMap = new HashMap();
            hashMap.put("timeData", timeDataList);
            hashMap.put("titleData", ManagementCurriculumPackageTitleData());
            return Message.success(null).add(hashMap);
        } else return Message.success("该学生没有上课班级！");
    }

    @Override
    public Message selectByClassId(int classId) {
        return Message.success(null).add(scheduleMapper.selectByClassId(classId));
    }

    @Override
    public Message cheakingTeacher(Date day, int timeSkcd, int timeSkjc) {
        List<Staff> TeacherList = staffMapper.selectAllByStaffJurisdiction(8);
        List teacher = new ArrayList();
        List<Schedule> scheduleList = scheduleMapper.selectByTimeDay(day);
        for (Schedule schedule : scheduleList) {
            if (schedule.getTimeSkcd() <= timeSkcd && schedule.getTimeSkcd() + schedule.getTimeSkjc() >= timeSkcd || schedule.getTimeSkcd() >= timeSkcd && timeSkcd + timeSkjc >= schedule.getTimeSkcd() && timeSkcd + timeSkjc <= schedule.getTimeSkcd() + schedule.getTimeSkjc() || schedule.getTimeSkcd() >= timeSkcd && timeSkcd + timeSkjc >= schedule.getTimeSkcd() + schedule.getTimeSkjc()) {
                teacher.add(schedule.getTeacherId());
            }
        }
        teacher = duplicate(teacher);
        if (teacher.size() == 0) {
            return Message.success(null).add(TeacherList);
        } else {
            List<Staff> temp = new ArrayList<>();
            for (Staff staff : TeacherList) {
                for (int i = 0; i < teacher.size(); i++) {
                    if (staff.getStaffId().equals(teacher.get(i))) {
                        temp.add(staff);
                    }
                }
            }
            temp = duplicate(temp);
            TeacherList.removeAll(temp);
            return Message.success(null).add(TeacherList);
        }
    }

    @Override
    public Message cheakingRoom(Date day, int timeSkcd, int timeSkjc) {
        List<Room> roomList = roomMapper.selectAll();
        List room = new ArrayList();
        List<Schedule> scheduleList = scheduleMapper.selectByTimeDay(day);
        for (Schedule schedule : scheduleList) {
            if (schedule.getTimeSkcd() <= timeSkcd && schedule.getTimeSkcd() + schedule.getTimeSkjc() >= timeSkcd || schedule.getTimeSkcd() >= timeSkcd && timeSkcd + timeSkjc >= schedule.getTimeSkcd() && timeSkcd + timeSkjc <= schedule.getTimeSkcd() + schedule.getTimeSkjc() || schedule.getTimeSkcd() >= timeSkcd && timeSkcd + timeSkjc >= schedule.getTimeSkcd() + schedule.getTimeSkjc()) {
                room.add(schedule.getRoomId());
            }
        }
        room = duplicate(room);
        if (room.size() == 0) {
            return Message.success(null).add(roomList);
        } else {
            List<Room> temp = new ArrayList<>();
            for (Room room1 : roomList) {
                for (int i = 0; i < room.size(); i++) {
                    if (room1.getRoomId().equals(room.get(i))) {
                        temp.add(room1);
                    }
                }
            }
            temp = duplicate(temp);
            roomList.removeAll(temp);
            return Message.success(null).add(roomList);
        }
    }


    public HashMap ManagementCurriculumPackageTimeData(int i, List<Schedule> scheduleList) {
        HashMap hashMap = new HashMap();
        hashMap.put("id", i);
        hashMap.put("label", GetEveryDate.getWeekDayBynumber(i));
        for (Schedule schedule : scheduleList) {
            int timeStart = schedule.getTimeSkcd();
            int timeSkjc = schedule.getTimeSkjc();
            int timeEnd = timeStart + timeSkjc - 1;
            String clazzName = schedule.getClazz().getClassName();
            String TeacherName = staffMapper.selectByPrimaryKey(schedule.getTeacherId()).getStaffName();
            for (int j = timeStart; j <= timeEnd; j++) {
                switch (j) {
                    case 1: {
                        hashMap.put("oneS", clazzName);
                        hashMap.put("oneT", TeacherName);
                        break;
                    }
                    case 2: {
                        hashMap.put("twoS", clazzName);
                        hashMap.put("twoT", TeacherName);
                        break;
                    }
                    case 3: {
                        hashMap.put("threeS", clazzName);
                        hashMap.put("threeT", TeacherName);
                        break;
                    }
                    case 4: {
                        hashMap.put("fourS", clazzName);
                        hashMap.put("fourT", TeacherName);
                        break;
                    }
                    case 5: {
                        hashMap.put("fiveS", clazzName);
                        hashMap.put("fiveT", TeacherName);
                        break;
                    }
                    case 6: {
                        hashMap.put("sixS", clazzName);
                        hashMap.put("sixT", TeacherName);
                        break;
                    }
                    case 7: {
                        hashMap.put("sevenS", clazzName);
                        hashMap.put("sevenT", TeacherName);
                        break;
                    }
                    case 8: {
                        hashMap.put("eightS", clazzName);
                        hashMap.put("eightT", TeacherName);
                        break;
                    }
                    case 9: {
                        hashMap.put("nineS", clazzName);
                        hashMap.put("nineT", TeacherName);
                        break;
                    }
                    case 10: {
                        hashMap.put("tenS", clazzName);
                        hashMap.put("tenT", TeacherName);
                        break;
                    }
                    case 11: {
                        hashMap.put("elevenS", clazzName);
                        hashMap.put("elevenT", TeacherName);
                        break;
                    }
                    case 12: {
                        hashMap.put("twelveS", clazzName);
                        hashMap.put("twelveT", TeacherName);
                        break;
                    }

                }
            }
        }

        return hashMap;
    }

    public List<Map> ManagementCurriculumPackageTitleData() {
        List<Map> mapList = new LinkedList<>();
        List<ScheduleDataNo> scheduleDataNoList = scheduleDataNoMapper.selectALL();
        for (ScheduleDataNo scheduleDataNo : scheduleDataNoList) {
            HashMap hashMap = new HashMap();
            hashMap.put("id", scheduleDataNo.getScheduleDataNoId());
            hashMap.put("count", scheduleDataNo.getScheduleDataNoId());
            switch (scheduleDataNo.getScheduleDataNoRange()) {
                case "morning":
                    hashMap.put("label", "早上");
                    break;
                case "afternoon":
                    hashMap.put("label", "下午");
                    break;
                case "night":
                    hashMap.put("label", "晚上");
                    break;
            }
            hashMap.put("starttime", scheduleDataNo.getScheduleDataNoDateTextStart());
            hashMap.put("endTime", scheduleDataNo.getScheduleDataNoDateTextEnd());
            mapList.add(hashMap);
        }
        return mapList;
    }


    @Override
    public Message selectappletByTeacherIdAndClassId(int teacherId, int classId) {
        List<Schedule> scheduleList = scheduleMapper.selectByTeacherIdAndTimeDayNowAndClassId(teacherId, classId);
        HashMap hashMap = new HashMap();
        List<Map> weekList = weekListInit();
        hashMap.put("weekList", weekList);
        List<Map> morning = dataNoRangeListInit("morning");
        hashMap.put("morning", morning);
        List<Map> afternoon = dataNoRangeListInit("afternoon");
        hashMap.put("afternoon", afternoon);
        List<Map> night = dataNoRangeListInit("night");
        hashMap.put("night", night);
        List<Map> tagList = ClassIdtagListInit(teacherId, classId);
        hashMap.put("tagList", tagList);
        List<Map> wlist = wlistInit(scheduleList);
        hashMap.put("wlist", wlist);
        return Message.success(null).add(hashMap);
    }

    @Override
    public Message selectClassStatusByScheduleId(int id) throws ParseException {
        Schedule schedule = scheduleMapper.selectByPrimaryKey(id);
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        String riqi = ft.format(schedule.getTimeDay());

        String timestartToString = riqi + " " + scheduleDataNoMapper.selectByPrimaryKey(schedule.getTimeSkcd()).getScheduleDataNoDateTextStart();
        int timeEndId = schedule.getTimeSkcd() + schedule.getTimeSkjc() - 1;
        String timeEndToString = riqi + " " + scheduleDataNoMapper.selectByPrimaryKey(timeEndId).getScheduleDataNoDateTextEnd();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date timeStart = simpleDateFormat.parse(timestartToString);
        Date timeEnd = simpleDateFormat.parse(timeEndToString);
        Date date = new Date();
        int compareTo = date.compareTo(timeStart);
        if (compareTo == -1) {
            return Message.success("课程尚未开始");
        } else if (compareTo == 0)
            return Message.success("课程进行中");
        else if (compareTo == 1) {
            int compareTo2 = date.compareTo(timeEnd);
            if (compareTo2 == -1) {
                return Message.success("课程进行中");
            } else if (compareTo2 == 0) {
                return Message.success("课程进行中");
            } else if (compareTo2 == 1) {
                return Message.success("课程结束");
            }
        }
        return Message.fail("判断出现问题，请联系系统管理员");
    }


    public List<Map> weekListInit() {
        Date date = new Date();
        String today = new SimpleDateFormat("MM/dd").format(date);
        List<Map> mapList = new LinkedList<>();
        List<String> stringList = GetEveryDate.dateToWeek(date);
        int i = 1;
        for (String day : stringList) {
            HashMap map = new HashMap();
            switch (i) {
                case 1:
                    map.put("weekName", "周一");
                    break;
                case 2:
                    map.put("weekName", "周二");
                    break;
                case 3:
                    map.put("weekName", "周三");
                    break;
                case 4:
                    map.put("weekName", "周四");
                    break;
                case 5:
                    map.put("weekName", "周五");
                    break;
                case 6:
                    map.put("weekName", "周六");
                    break;
                case 7:
                    map.put("weekName", "周日");
                    break;
            }
            i++;
            map.put("week_text", day);
            if (today.equals(day))
                map.put("today", 1);
            else map.put("today", 0);
            mapList.add(map);
        }
        return mapList;
    }

    public List<Map> dataNoRangeListInit(String range) {
        List<ScheduleDataNo> scheduleDataNoList = scheduleDataNoMapper.selectByScheduleDataNoRange(range);
        List<Map> mapList = new LinkedList<>();
        for (ScheduleDataNo scheduleDataNo : scheduleDataNoList) {
            HashMap hashMap = new HashMap();
            hashMap.put("dataNo", scheduleDataNo.getScheduleDataNoId());
            hashMap.put("data_text", scheduleDataNo.getScheduleDataNoDateTextStart() + " " + scheduleDataNo.getScheduleDataNoDateTextEnd());
            mapList.add(hashMap);
        }
        return mapList;
    }

    public List<Map> AlltagListInit(int teacherId) {
        List<Map> mapList = new LinkedList<>();
        HashMap map = new HashMap();
        map.put("id", -1);
        map.put("tagName", "我的课程");
        map.put("isCourse", 1);
        mapList.add(map);
        List<Clazz> clazzList = clazzMapper.selectByClassTeacherIdAndStatus01(teacherId);
        for (Clazz clazz : clazzList) {
            HashMap map1 = new HashMap();
            map1.put("id", clazz.getClassId());
            map1.put("tagName", clazz.getClassName());
            map1.put("isCourse", 0);
            mapList.add(map1);
        }
        return mapList;
    }

    public List<Map> ClassIdtagListInit(int teacherId, int classId) {
        List<Map> mapList = new LinkedList<>();
        HashMap map = new HashMap();
        map.put("id", -1);
        map.put("tagName", "我的课程");
        map.put("isCourse", 0);
        mapList.add(map);
        List<Clazz> clazzList = clazzMapper.selectByClassTeacherIdAndStatus01(teacherId);
        for (Clazz clazz : clazzList) {
            HashMap map1 = new HashMap();
            map1.put("id", clazz.getClassId());
            map1.put("tagName", clazz.getClassName());
            if (clazz.getClassId() == classId)
                map1.put("isCourse", 1);
            else
                map1.put("isCourse", 0);
            mapList.add(map1);
        }
        return mapList;
    }

    public List<Map> wlistInit(List<Schedule> scheduleList) {
        List<Map> mapList = new LinkedList<>();
        for (Schedule schedule : scheduleList) {
            HashMap hashMap = new HashMap();
            hashMap.put("roomName", schedule.getRoom().getRoomLoc());
            hashMap.put("xqj", schedule.getWeekName());
            hashMap.put("skjc", schedule.getTimeSkjc());
            hashMap.put("skcd", schedule.getTimeSkcd());
            hashMap.put("kcmc", schedule.getClazz().getClassName());
            hashMap.put("color", schedule.getScheduleColor());
            hashMap.put("textColor", schedule.getFontColor());
            mapList.add(hashMap);
        }
        return mapList;
    }


    @Override
    public int batchInsert(List<Schedule> list) {
        return scheduleMapper.batchInsert(list);
    }

    @Override
    public Message test(int classId) {
        List<Schedule> scheduleList = scheduleMapper.selectByClassIdAndNowOrderByTimeSkcdAsc(classId);
        System.out.println(scheduleList);
        if (scheduleList.size() > 0) {
            for (Schedule schedule : scheduleList) {
                if (schedule.getScheduleStatus() == 0) {
                    return scheduleMapper.updateScheduleStatusByScheduleId(1, schedule.getScheduleId()) > 0 ? Message.success(null) : Message.fail(null);
                }
            }
        } else return Message.fail(null);
        return Message.fail(null);
    }

    /**
     * 对list的数据的的obj 进行去重
     *
     * @param list
     * @return
     */
    private List duplicate(List list) {
        List listTemp = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            if (!listTemp.contains(list.get(i))) {
                listTemp.add(list.get(i));
            }
        }
        return listTemp;
    }

    @Override
    public int updateBatch(List<Schedule> list) {
        return scheduleMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<Schedule> list) {
        return scheduleMapper.updateBatchSelective(list);
    }


    @Override
    public Message roomScheduleOfTheDayInDay() {
        //获得所有房间
        List<Room> roomList = roomMapper.selectAll();
        //得到最后返回的Map
        HashMap returnMap = new HashMap();
        Date now = new Date();
        List<Map> roomDataList= new LinkedList<>();
        int i=1;
        for (Room room:roomList){
            List<Schedule> scheduleList = scheduleMapper.selectAllByRoomIdAndTimeDay(room.getRoomId(),now);
            HashMap map1 = ManagementCurriculumPackageRoomData(i, scheduleList,room.getRoomLoc());
            i++;
            roomDataList.add(map1);
        }
        returnMap.put("roomData", roomDataList);
        returnMap.put("titleData", ManagementCurriculumPackageTitleData());
        return Message.success(null).add(returnMap);
    }


    private HashMap ManagementCurriculumPackageRoomData(int i, List<Schedule> scheduleList,String roomLoc) {
        HashMap hashMap = new HashMap();
        hashMap.put("id", i);
        hashMap.put("label", roomLoc);
        for (Schedule schedule : scheduleList) {
            int timeStart = schedule.getTimeSkcd();
            int timeSkjc = schedule.getTimeSkjc();
            int timeEnd = timeStart + timeSkjc - 1;

            Clazz clazz = clazzMapper.selectByPrimaryKey(schedule.getClassId());

            String ClazzTypeName="暂无分类";
            if (clazz.getClazz_type().getClazzTypeName()!=null){
                ClazzTypeName = clazz.getClazz_type().getClazzTypeName();
            }

            String TTName="暂无分类";
            Lesson lesson = lessonMapper.selectByPrimaryKey(schedule.getClazz().getClassCourseId());
            if (lesson!=null){
                if (lesson.getTechnicalType().getTechnicalTypeName()!=null){
                    TTName= lesson.getTechnicalType().getTechnicalTypeName();
                }
            }


            String clazzName="暂无班级名称";
            if (schedule.getClazz().getClassName()!=null){
            clazzName = schedule.getClazz().getClassName();
            }

            String TeacherName = staffMapper.selectByPrimaryKey(schedule.getTeacherId()).getStaffName();
            for (int j = timeStart; j <= timeEnd; j++) {
                switch (j) {
                    case 1: {
                        hashMap.put("oneCT", ClazzTypeName);
                        hashMap.put("oneTT", TTName);
                        hashMap.put("oneS", clazzName);
                        hashMap.put("oneT", TeacherName);
                        break;
                    }
                    case 2: {
                        hashMap.put("twoCT", ClazzTypeName);
                        hashMap.put("twoTT", TTName);
                        hashMap.put("twoS", clazzName);
                        hashMap.put("twoT", TeacherName);
                        break;
                    }
                    case 3: {
                        hashMap.put("threeCT", ClazzTypeName);
                        hashMap.put("threeTT", TTName);
                        hashMap.put("threeS", clazzName);
                        hashMap.put("threeT", TeacherName);
                        break;
                    }
                    case 4: {
                        hashMap.put("fourCT", ClazzTypeName);
                        hashMap.put("fourTT", TTName);
                        hashMap.put("fourS", clazzName);
                        hashMap.put("fourT", TeacherName);
                        break;
                    }
                    case 5: {
                        hashMap.put("fiveCT", ClazzTypeName);
                        hashMap.put("fiveTT", TTName);
                        hashMap.put("fiveS", clazzName);
                        hashMap.put("fiveT", TeacherName);
                        break;
                    }
                    case 6: {
                        hashMap.put("sixCT", ClazzTypeName);
                        hashMap.put("sixTT", TTName);
                        hashMap.put("sixS", clazzName);
                        hashMap.put("sixT", TeacherName);
                        break;
                    }
                    case 7: {
                        hashMap.put("sevenCT", ClazzTypeName);
                        hashMap.put("sevenTT", TTName);
                        hashMap.put("sevenS", clazzName);
                        hashMap.put("sevenT", TeacherName);
                        break;
                    }
                    case 8: {
                        hashMap.put("eightCT", ClazzTypeName);
                        hashMap.put("eightTT", TTName);
                        hashMap.put("eightS", clazzName);
                        hashMap.put("eightT", TeacherName);
                        break;
                    }
                    case 9: {
                        hashMap.put("nineCT", ClazzTypeName);
                        hashMap.put("nineTT", TTName);
                        hashMap.put("nineS", clazzName);
                        hashMap.put("nineT", TeacherName);
                        break;
                    }
                    case 10: {
                        hashMap.put("tenCT", ClazzTypeName);
                        hashMap.put("tenTT", TTName);
                        hashMap.put("tenS", clazzName);
                        hashMap.put("tenT", TeacherName);
                        break;
                    }
                    case 11: {
                        hashMap.put("elevenCT", ClazzTypeName);
                        hashMap.put("elevenTT", TTName);
                        hashMap.put("elevenS", clazzName);
                        hashMap.put("elevenT", TeacherName);
                        break;
                    }
                    case 12: {
                        hashMap.put("twelveCT", ClazzTypeName);
                        hashMap.put("twelveTT", TTName);
                        hashMap.put("twelveS", clazzName);
                        hashMap.put("twelveT", TeacherName);
                        break;
                    }

                }
            }
        }

        return hashMap;
    }

    /**
    * @Author: Shj
    * @Date: 22:28 2021/1/3
    * @Description: 教师排课表
    */
    @Override
    public Message teacherSchedule() {
        List<Map> timeDataList = new LinkedList<>();
        List<Staff> staffAllList = staffMapper.countAllStaffWithJurisdictionInSixAndSevenAndEight();
        int i = 1;
        for (Staff staff : staffAllList) {
            List<Schedule> scheduleList = scheduleMapper.selectByTeacherIdAndTimeDayNow(staff.getStaffId());
            HashMap map1 = ManagementCurriculumPackageTimeData2(i, staff.getStaffName(), scheduleList);
            i++;
            timeDataList.add(map1);
        }
        HashMap hashMap = new HashMap();
        hashMap.put("timeData", timeDataList);
        hashMap.put("titleData", ManagementCurriculumPackageTitleData());
        return Message.success(null).add(hashMap);
    }

    @Override
    public Message resourcesScheduleOfTheDayInDay() {
        //获得所有房间
        List<Room> roomList = roomMapper.selectAll();
        //得到最后返回的Map
        HashMap returnMap = new HashMap();
        Date now = new Date();
        List<Map> roomDataList= new LinkedList<>();
        int i=1;
        for (Room room:roomList){
            List<Schedule> scheduleList = scheduleMapper.selectAllByRoomIdAndTimeDay(room.getRoomId(),now);
            HashMap map1 = ManagementCurriculumPackageResourcesData(i, scheduleList,room.getRoomLoc());
            i++;
            roomDataList.add(map1);
        }
        returnMap.put("roomData", roomDataList);
        returnMap.put("titleData", ManagementCurriculumPackageTitleData());
        return Message.success(null).add(returnMap);
    }




    public HashMap ManagementCurriculumPackageTimeData2(int i,String teacherName , List<Schedule> scheduleList) {
        HashMap hashMap = new HashMap();
        hashMap.put("id", i);
        hashMap.put("label", teacherName);
        for (Schedule schedule : scheduleList) {
            int timeStart = schedule.getTimeSkcd();
            int timeSkjc = schedule.getTimeSkjc();
            int timeEnd = timeStart + timeSkjc - 1;
            Clazz clazz = clazzMapper.selectByPrimaryKey(schedule.getClassId());
            Room room = roomMapper.selectByPrimaryKey(schedule.getRoomId());
            String CTName="暂无分类";
            String TTName="暂无分类";
            String clazzName="暂无班级名称";
            String roomName="暂无教室信息";
            if (clazz!=null){
                if (clazz.getClazz_type().getClazzTypeName()!=null){
                    CTName = clazz.getClazz_type().getClazzTypeName();
                }
                Lesson lesson = lessonMapper.selectByPrimaryKey(schedule.getClazz().getClassCourseId());
                if (lesson!=null){
                    if (lesson.getTechnicalType().getTechnicalTypeName()!=null){
                        TTName= lesson.getTechnicalType().getTechnicalTypeName();
                    }
                }
                if (schedule.getClazz().getClassName()!=null){
                    clazzName = schedule.getClazz().getClassName();
                }
            }
            if (room!=null){
                roomName=room.getRoomLoc();
            }
            for (int j = timeStart; j <= timeEnd; j++) {
                switch (j) {
                    case 1: {
                        hashMap.put("oneCN", clazzName);
                        hashMap.put("oneCT", CTName);
                        hashMap.put("oneTT", TTName);
                        hashMap.put("oneRN", roomName);
                        break;
                    }
                    case 2: {
                        hashMap.put("twoCN", clazzName);
                        hashMap.put("twoCT", CTName);
                        hashMap.put("twoTT", TTName);
                        hashMap.put("twoRN", roomName);
                        break;
                    }
                    case 3: {
                        hashMap.put("threeCN", clazzName);
                        hashMap.put("threeCT", CTName);
                        hashMap.put("threeTT", TTName);
                        hashMap.put("threeRN", roomName);
                        break;
                    }
                    case 4: {
                        hashMap.put("fourCN", clazzName);
                        hashMap.put("fourCT", CTName);
                        hashMap.put("fourTT", TTName);
                        hashMap.put("fourRN", roomName);
                        break;
                    }
                    case 5: {
                        hashMap.put("fiveCN", clazzName);
                        hashMap.put("fiveCT", CTName);
                        hashMap.put("fiveTT", TTName);
                        hashMap.put("fiveRN", roomName);
                        break;
                    }
                    case 6: {
                        hashMap.put("sixCN", clazzName);
                        hashMap.put("sixCT", CTName);
                        hashMap.put("sixTT", TTName);
                        hashMap.put("sixRN", roomName);
                        break;
                    }
                    case 7: {
                        hashMap.put("sevenCN", clazzName);
                        hashMap.put("sevenCT", CTName);
                        hashMap.put("sevenTT", TTName);
                        hashMap.put("sevenRN", roomName);
                        break;
                    }
                    case 8: {
                        hashMap.put("eightCN", clazzName);
                        hashMap.put("eightCT", CTName);
                        hashMap.put("eightTT", TTName);
                        hashMap.put("eightRN", roomName);
                        break;
                    }
                    case 9: {
                        hashMap.put("nineCN", clazzName);
                        hashMap.put("nineCT", CTName);
                        hashMap.put("nineTT", TTName);
                        hashMap.put("nineRN", roomName);
                        break;
                    }
                    case 10: {
                        hashMap.put("tenCN", clazzName);
                        hashMap.put("tenCT", CTName);
                        hashMap.put("tenTT", TTName);
                        hashMap.put("tenRN", roomName);
                        break;
                    }
                    case 11: {
                        hashMap.put("elevenCN", clazzName);
                        hashMap.put("elevenCT", CTName);
                        hashMap.put("elevenTT", TTName);
                        hashMap.put("elevenRN", roomName);
                        break;
                    }
                    case 12: {
                        hashMap.put("twelveCN", clazzName);
                        hashMap.put("twelveCT", CTName);
                        hashMap.put("twelveTT", TTName);
                        hashMap.put("twelveRN", roomName);
                        break;
                    }

                }
            }
        }

        return hashMap;
    }

    private HashMap ManagementCurriculumPackageResourcesData(int i, List<Schedule> scheduleList,String roomLoc) {
        HashMap hashMap = new HashMap();
        hashMap.put("id", i);
        hashMap.put("label", roomLoc);
        for (Schedule schedule : scheduleList) {
            int timeStart = schedule.getTimeSkcd();
            int timeSkjc = schedule.getTimeSkjc();
            int timeEnd = timeStart + timeSkjc - 1;
            String TeacherName = staffMapper.selectByPrimaryKey(schedule.getTeacherId()).getStaffName();
            for (int j = timeStart; j <= timeEnd; j++) {
                switch (j) {
                    case 1: {
                        hashMap.put("oneT", TeacherName);
                        break;
                    }
                    case 2: {
                        hashMap.put("twoT", TeacherName);
                        break;
                    }
                    case 3: {
                        hashMap.put("threeT", TeacherName);
                        break;
                    }
                    case 4: {
                        hashMap.put("fourT", TeacherName);
                        break;
                    }
                    case 5: {
                        hashMap.put("fiveT", TeacherName);
                        break;
                    }
                    case 6: {
                        hashMap.put("sixT", TeacherName);
                        break;
                    }
                    case 7: {
                        hashMap.put("sevenT", TeacherName);
                        break;
                    }
                    case 8: {
                        hashMap.put("eightT", TeacherName);
                        break;
                    }
                    case 9: {
                        hashMap.put("nineT", TeacherName);
                        break;
                    }
                    case 10: {
                        hashMap.put("tenT", TeacherName);
                        break;
                    }
                    case 11: {
                        hashMap.put("elevenT", TeacherName);
                        break;
                    }
                    case 12: {
                        hashMap.put("twelveT", TeacherName);
                        break;
                    }

                }
            }
        }
        return hashMap;
    }


    @Override
    public Message batchInsertAccordingToTheDayOfTheWeek(Schedule schedule, int times, String begindateToString) throws ParseException {
        if (begindateToString!=null){
            int weekname =GetEveryDate.GetWeekByDate(begindateToString);
            schedule.setWeekName(weekname);
            if (weekname==6||weekname==7)
                schedule.setScheduleWorkOvertime(1);
            else {
                //第十节课是晚上5点开始，算加班
                if (schedule.getTimeSkcd()>=9)
                    schedule.setScheduleWorkOvertime(1);
                else {
                    schedule.setScheduleWorkOvertime(0);
                }
            }
        }
        List<Schedule> scheduleList = new ArrayList<>();
        for (int i=0;i<times;i++){
            schedule.setTimeDay(GetEveryDate.getAfterDate(i*7,begindateToString));
            Schedule schedule1 = new Schedule();
            BeanUtils.copyProperties(schedule,schedule1);
            scheduleList.add(schedule1);
        }
        /*
        System.out.println(scheduleList.get(0).getTimeDay());
        System.out.println(scheduleList.get(1).getTimeDay());
        System.out.println(scheduleList.get(2).getTimeDay());
        System.out.println(scheduleList.get(3).getTimeDay());
        System.out.println(scheduleList.get(4).getTimeDay());
         return Message.success(null);
        */
       return scheduleMapper.batchInsert(scheduleList)>0 ?Message.success(null):Message.fail(null);

    }
 /*
    @Override
    public Message batchInsertAccordingToTheDayOfTheWeek(Schedule schedule, int times, String begindate) throws ParseException {
        List<Date> dateList = new LinkedList<>();
        for (int i=0;i<times;i++){
            dateList.add(GetEveryDate.getAfterDate(i*7,begindate));
        }
        return Message.success(null).add(dateList);
    }*/
}







